import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/res.dart';
import 'package:huajianshe_app/pages/search/SearchPage.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';

class SearchAppBar extends StatefulWidget implements PreferredSizeWidget{

  final SearchType searchType;
  final void Function(String text, bool reset) doSearch;
  final String? text;

  const SearchAppBar(this.searchType, this.doSearch, {Key? key, this.text}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SearchAppBarState();
  }

  @override
  Size get preferredSize => Size.fromHeight(48);
}

class _SearchAppBarState extends BaseState<SearchAppBar> {

  TextEditingController searchController = TextEditingController();
  FocusNode searchNode = FocusNode();
  bool _showClearButton = false;

  @override
  void initState() {
    super.initState();

    searchController.addListener(() {
      // String text = searchController.text;
      // bool showClearButton = text.isNotEmpty;
      // print('$showClearButton');
      // if (showClearButton != _showClearButton) {
      //   setState(() => _showClearButton = showClearButton);
      // }
    });
  }

  @override
  Widget build(BuildContext context) {
    searchController.text = widget.text ?? '';
    return AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.dark,
        child: Material(color: Colors.white,
          child: SafeArea(
            child: Padding(padding: EdgeInsets.only(bottom: 10), child: Row(mainAxisSize: MainAxisSize.max, children: [
              W(13),
              Expanded(
                  child: Stack(children: [
                    ConstrainedBox(constraints: BoxConstraints(maxHeight: 45), child:
                    TextField(
                      autofocus: false,
                      controller: searchController,
                      focusNode: searchNode,
                      textInputAction: TextInputAction.search,
                      onSubmitted: (value) {
                        print('submit $value');
                        widget.doSearch(value, true);
                      },
                      decoration: InputDecoration(
                        isDense: true,
                        contentPadding: EdgeInsets.only(left: 30, right: 10, top: 5, bottom: 5),
                        fillColor: C.grayF6,
                        filled: true,
                        border: new OutlineInputBorder(
                          borderSide: BorderSide.none,
                            borderRadius: const BorderRadius.all(const Radius.circular(15.0)),
                          ),
                        hintStyle: TextStyle(fontSize: 13, color: C.gray666),
                        hintText: getSearchHint(),
                      ),
                    )),

                    if (_showClearButton)
                      Positioned.fill(child: Align(alignment: Alignment.centerRight, child: GestureDetector(onTap: clear,
                          child:  LoadImage('ic_delete_media'))), right: 8,),
                    Positioned.fill(child: Align(alignment: Alignment.centerLeft, child: LoadImage('ic_search')), left: 12,)

                  ])
              ),
              W(13),
              SmartButton(text: '取消', fontSize: 13, textColor: C.gray1A, width: 28, height: 20,
                backgroundColor: Colors.transparent, borderWidth: 0, borderColor: Colors.transparent, onPressed: () => AppNavigator.pop(context),),
              W(13)

            ]))
          ),
        ));
  }

  // doSearch(String text) {
  //   String text = searchController.text;
  //   if (text.isEmpty) {
  //     return;
  //   }
  //
  // }
  String getSearchHint() {
    switch(widget.searchType) {
      case SearchType.moment:
        return '搜索动态';
      case SearchType.circle:
        return '搜索圈子';
      case SearchType.friend:
        return '搜索好友';
      case SearchType.lesson:
        return '搜索课程';
      default:
        return '搜索';
    }
  }

  clear() {
    searchController.text = '';
  }
}